document.addEventListener('DOMContentLoaded', async () => {
  const {
    data
  } = await axios.get('/dashboard')
  console.log(data)
  for (let k in data.overview) {
    document.querySelector(`[name=${k}]`).innerHTML = data.overview[k]
  }
  initYearChart (data.year)
  initSalaryChart (data.salaryData)
  initClassChart (data.groupData[1])
  // console.log(data.groupData[1])

  const btns = document.querySelector('#btns')
  btns.addEventListener('click',function (e) {
    console.log(e.target.tagName)
    if(e.target.tagName === 'BUTTON') {
      btns.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')
      initClassChart (data.groupData[e.target.innerHTML])
    }
  })

  initGenderChart (data.salaryData)
  initMapChart (data.provinceData)
})

function initYearChart (year) {
  const myChart = echarts.init(document.querySelector('.col-xl-8 #line'))
  const option = {
    title: {
      text: '2022全学科薪资走势',
      top: 15,
      left: 15,
    },
    xAxis: {
      type: 'category',
      //year.map(itme => itme.month)
      data: year.map(item => item.month),
      //xAxis.axisLine.lineStyle. type  = 'solid'
      axisLine: {
        lineStyle: {
          color: '#CECFCF',
          type: 'dashed',
          // dashOffset: 10,
        }
      },
      axisLabel: {
        show: true,
        color: '#999',
      },

    },
    yAxis: {
      show: true,
      type: 'value',
      splitNumber: 5,
      splitLine: {
        show: true,
        lineStyle: {
          color: ['#999'],
          type: 'dashed',
        },
      },
    },
    series: [{
      data: year.map(item => item.salary),
      type: 'line',
      symbolSize: 10,
      smooth: true,
      lineStyle: {
        width: 4
      },
      areaStyle: {
        color: {
          type: 'linear',
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [{
            offset: 0,
            color: '#3513E0' // 0% 处的颜色
          }, {
            offset: 0.54,
            color: '#DDD6F9' // 0% 处的颜色
          }, {
            offset: 1,
            color: 'white' // 100% 处的颜色
          }],
          global: false // 缺省为 false
        },
      },
    }, ],
    tooltip: {
      trigger: 'axis'
    },

    color: {
      type: 'linear',
      x: 0,
      y: 1,
      x2: 1,
      y2: 0,
      colorStops: [{
        offset: 0,
        color: 'blue' // 0% 处的颜色
      }, {
        offset: 1,
        color: 'blue' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    },

  }
  option && myChart.setOption(option)
}
function initSalaryChart (salaryData) {
  const myChart = echarts.init(document.querySelector('.col-xl-4 #salary'))
  const option ={
    title:{
      text: '班级薪资分布',
      top: 15,
      left: 15,
    },
    tooltip: {
      trigger: 'item'
    },
    legend: {
      // show: false,
      bottom: '5%',
      left: 'center'
    },
    color: ['#FDA224','#5097FF','#3ABCFA','#35D89D'],
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['55%', '70%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 5,
          borderColor: '#fff',
          borderWidth: 2
        },
        label: {
          show: false,
          position: 'center'
        },
        // emphasis: {
        //   label: {
        //     show: true,
        //     fontSize: '20',
        //     fontWeight: 'bold'
        //   }
        // },
        labelLine: {
          show: false
        },
        /* salaryData: Array(4)
          0: {label: '1万以下', g_count: 0, b_count: 2}
          1: {label: '1-1.5万', g_count: 12, b_count: 8}
          2: {label: '1.5-2万', g_count: 16, b_count: 13}
          3: {label: '2万以上', g_count: 13, b_count: 16} 
        */
       /*  data: [
          { value: 200, name: 'Search Engine' },
          { value: 735, name: 'Direct' },
          { value: 580, name: 'Email' },
          { value: 484, name: 'Union Ads' }
         */
        data: salaryData.map(function(item){
          return{
            value: item.g_count + item.b_count,
            name: item.label
          }
        })
      }
    ]
  }
  option && myChart.setOption(option)
}
function initClassChart (groupData) {
  const myChart = echarts.init(document.querySelector('.card #lines'))
  const option = {
    tooltip: {
      trigger: 'item',
      // axisPointer: {
      //   type: 'cross',
      //   label: {
      //     backgroundColor: '#6a7985'
      //   }
      // }
    },
    xAxis: {
      type: 'category',
      //data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      
      data: groupData.map(function(i){
        return i.name
      }),
     
      // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      axisLine: {
          lineStyle: {
            color: '#CECFCF',
            type: 'dashed',
            // dashOffset: 10,
          }
        },
        axisLabel: {
          show: true,
          color: '#999',
        },
    },
    yAxis: {
      type: 'value',
      splitLine: {
          show: true,
          lineStyle: {
            color: ['#E7ECF4'],
            type: 'dashed',
          },
        },
    },
    grid: {
      top: '10',
      left: '80',
      right: '20',
      bottom: '50',
    },
    color:[{
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
          offset: 0, color: '#3FD69F' // 0% 处的颜色
      }, {
          offset: 1, color: '#CDF4E6' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    },{
      type: 'linear',
      x: 0,
      y: 0,
      x2: 0,
      y2: 1,
      colorStops: [{
          offset: 0, color: '#4EA2EF' // 0% 处的颜色
      }, {
          offset: 1, color: '#D9ECFC' // 100% 处的颜色
      }],
      global: false // 缺省为 false
    }],
    

    series: [
      {
        name: '期望薪资',
        data: groupData.map(function(i) {
          return i.hope_salary
        }),
        type: 'bar',
        
      },
      {
        name: '实际薪资',
        data: groupData.map(function(i) {
          return i.salary
        }),
        type: 'bar',
        
      },
    ]
  }
  option && myChart.setOption(option)
}
function initGenderChart (salaryData) {
  const myChart = echarts.init(document.querySelector('.col-xl-4 #gender'))
  const option = {
    color: ['#FDA224','#5097FF','#3ABCFA','#34D39A'],
    title:  [
      {
        text: '男女薪资分布',
        top: '10',
        left: '10',
        textStyle:{
          fontSize: '16'
        },
        
        
      },
      {
        text: '男生',
        top: '45%',
        left: '50%',
        textStyle:{
          fontSize: '12'
        },
        
      },
      {
        text: '女生',
        top: '85%',
        left: '50%',
        textStyle:{
          fontSize: '12'
        },
      },
    ],
    tooltip: {
      trigger: 'item'
    },
  
    
    series: [
      {
        name: '男生',
        type: 'pie',
        radius: ['20%','30%'],
        center: ['50%','30%'],
        // data: [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ],
        data: salaryData.map(function(i) {
          return {
            value: i.b_count,
            name: i.label
          }
        }),
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      },
      {
        name: '女生',
        type: 'pie',
        radius: ['20%','30%'],
        center: ['50%','70%'],
        data: salaryData.map(function(i) {
          return {
            value: i.g_count,
            name: i.label
          }
        }),
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }
  option && myChart.setOption(option)
}
const initMapChart = (data) => {
  const myEchart = echarts.init(document.querySelector('#map'))
  // const randomValue = () => {
  //   return parseInt(Math.random() * 20)
  // }
  // const dataList = [
  //   { name: '南海诸岛', value: 0 },
  //   { name: '北京', value: randomValue() },
  //   { name: '天津', value: randomValue() },
  //   { name: '上海', value: randomValue() },
  //   { name: '重庆', value: randomValue() },
  //   { name: '河北', value: randomValue() },
  //   { name: '河南', value: randomValue() },
  //   { name: '云南', value: randomValue() },
  //   { name: '辽宁', value: randomValue() },
  //   { name: '黑龙江', value: randomValue() },
  //   { name: '湖南', value: randomValue() },
  //   { name: '安徽', value: randomValue() },
  //   { name: '山东', value: randomValue() },
  //   { name: '新疆', value: randomValue() },
  //   { name: '江苏', value: randomValue() },
  //   { name: '浙江', value: randomValue() },
  //   { name: '江西', value: randomValue() },
  //   { name: '湖北', value: randomValue() },
  //   { name: '广西', value: randomValue() },
  //   { name: '甘肃', value: randomValue() },
  //   { name: '山西', value: randomValue() },
  //   { name: '内蒙古', value: randomValue() },
  //   { name: '陕西', value: randomValue() },
  //   { name: '吉林', value: randomValue() },
  //   { name: '福建', value: randomValue() },
  //   { name: '贵州', value: randomValue() },
  //   { name: '广东', value: randomValue() },
  //   { name: '青海', value: randomValue() },
  //   { name: '西藏', value: randomValue() },
  //   { name: '四川', value: randomValue() },
  //   { name: '宁夏', value: randomValue() },
  //   { name: '海南', value: randomValue() },
  //   { name: '台湾', value: randomValue() },
  //   { name: '香港', value: randomValue() },
  //   { name: '澳门', value: randomValue() },
  // ]
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  dataList.forEach((item) => {
    const obj = data.find((it) => it.name.replace(/省|回族自治区|吾尔自治区|壮族自治区|特别行政区|自治区/g, '') === item.name)
    if (obj) item.value = obj.value
  })
  let option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16,
      },
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff',
      },
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0'],
      },
      show: true,
      left: 40,
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)',
        },
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff',
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)',
        },
      },
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList,
      },
    ],
  }
  myEchart.setOption(option)
}

